package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private IStrategyService strategyService;

    @Override
    public void viewnumIncrease(String sid, int num) {
        //判断key是否存在
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        //统一加
        vo.setViewnum( vo.getViewnum() + num);
        //更新操作
        this.setStrategyStatisVo(vo);
    }
    @Override
    public StrategyStatisVO getStrategyStatisVo(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO vo = null;
        if(!template.hasKey(key)){
            //如果不存在, 初始化vo
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
            template.opsForValue().set(key, JSON.toJSONString(vo));
        }else{
            //如果存在直接+num
            String voStr = template.opsForValue().get(key);
            vo = JSON.parseObject(voStr, StrategyStatisVO.class);
        }
        return vo;
    }

    @Override
    public void setStrategyStatisVo(StrategyStatisVO vo) {





        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key, JSON.toJSONString(vo));
    }

    @Override
    public void replynumIncrease(String sid, int num) {
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        vo.setReplynum(vo.getReplynum() + num);
        this.setStrategyStatisVo(vo);
    }

    @Override
    public boolean favor(String sid, String uid) {
        //获取记号(攻略id集合)
        String signKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //记号不存在, 初始化
        List<String> sidList = new ArrayList<>();
        if(template.hasKey(signKey)){
            //记号存在, 直接获取
            String sidListStr = template.opsForValue().get(signKey);
            //将字符串转换成list集合, 参数2:集合泛型
            sidList = JSON.parseArray(sidListStr, String.class);
        }
        //通过记号判断当前操作时收藏还是取消收藏
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        if(sidList.contains(sid)){
            //取消收藏操作: 获取vo, 收藏数-1, 将sid移除出记号集合中
            vo.setFavornum(vo.getFavornum() - 1);
            sidList.remove(sid);
        }else{
            //收藏操作: 获取vo, 收藏数+1, 将sid加入到记号集合中
            vo.setFavornum(vo.getFavornum() + 1);
            sidList.add(sid);
        }
        //更新记号, 更新vo对象
        template.opsForValue().set(signKey, JSON.toJSONString(sidList));
        this.setStrategyStatisVo(vo);

        return sidList.contains(sid);
    }

    @Override
    public List<String> getUserStrategyFavor(String uid) {
        String signKey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //记号不存在, 初始化
        List<String> sidList = new ArrayList<>();
        if(template.hasKey(signKey)){
            //记号存在, 直接获取
            String sidListStr = template.opsForValue().get(signKey);
            //将字符串转换成list集合, 参数2:集合泛型
            sidList = JSON.parseArray(sidListStr, String.class);
        }
        return sidList;
    }

    @Override
    public boolean strategyThumbup(String sid, String uid) {
        //拼接记号key
        String signKey = RedisKeys.USER_STRATEGY_THUMB.join(uid, sid);
        //判断key是否存在
        if(!template.hasKey(signKey)){
            //不存在, 获取vo, 点赞数+1, 将记号缓存到redis中,设置过期时间
            StrategyStatisVO vo = this.getStrategyStatisVo(sid);
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            this.setStrategyStatisVo(vo);

            //有效时间: 今天最后一秒 - 当前时间
            Date now = new Date();
            Date end = DateUtil.getEndDate(now);

            Long time = DateUtil.getDateBetween(now, end);
            template.opsForValue().set(signKey, "1", time, TimeUnit.SECONDS);
            return true;
        }
        //存在,不做任何操作
        return false;
    }

    @Override
    public boolean isStrategyVoExist(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> queryStartegyVos() {
        //keys strategy_statis_vo:*
        String pattern = RedisKeys.STRATEGY_STATIS_VO.join("*");
        //所有vo对象的key值
        Set<String> voKeys = template.keys(pattern);

        List<StrategyStatisVO> list = new ArrayList<>();
        if(voKeys != null && voKeys.size() > 0){
            for (String key : voKeys) {
                //通过vokey获取vo对象
                String voStr = template.opsForValue().get(key);
                list.add(JSON.parseObject(voStr, StrategyStatisVO.class));
            }

        }
        return list;
    }
}
